//
//  ArticleDBTools.m
//  NC-Trendy
//
//  Created by LeungChaos on 16/2/26.
//  Copyright © 2016年 liang. All rights reserved.
//

#import "ArticleDBTools.h"

@implementation ArticleDBTools

+ (NSManagedObjectContext *)objContext {
    return [AppDelegate shareAppDelegate].managedObjectContext;
}

+ (NSFetchRequest *)fetchRequest {
    return [NSFetchRequest fetchRequestWithEntityName:@"CoreArticle"];
}

+ (NSEntityDescription *)entityDesc {
    return [NSEntityDescription entityForName:@"CoreArticle" inManagedObjectContext:[self objContext]];
}

+ (CoreArticle *)selectCoreArticleWithID:(NSString *)a_id errorInfo:(void(^)(NSError *error))errorBlock {
    NSError *error;
    NSFetchRequest *fetchRequest = [self fetchRequest];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"article_id = %@",a_id];
    NSArray *array = [[self objContext]executeFetchRequest:fetchRequest error:&error];
    if (errorBlock) {
        errorBlock(error);
    }
    if (array.count > 0) {
        CoreArticle *article = [array lastObject];
        return article;
    } else {
        return nil;
    }
}

+ (void)insertCoreArticleWithArticle:(NCArticle *)article errorInfo:(void(^)(NSError *error))errorBlock {
    NSError *error;
    NSFetchRequest *fetchRequest = [self fetchRequest];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"article_id = %@",article.article_id];
    NSArray *array = [[self objContext]executeFetchRequest:fetchRequest error:&error];
    if (array.count == 0) {
        CoreArticle *cArticle = [[CoreArticle alloc]initWithEntity:[self entityDesc] insertIntoManagedObjectContext:[self objContext]];
        cArticle.article_addtime = article.article_addtime;
        cArticle.article_author = article.article_author;
        cArticle.article_content = article.article_content;
        cArticle.article_id = article.article_id;
        cArticle.article_title = article.article_title;
        [[self objContext] insertObject:cArticle];
        [[self objContext] save:&error];
    } 
    if (errorBlock) {
        errorBlock(error);
    }

}

+ (void)deleteAllCoreArticleWithErrorInfo:(void(^)(NSError *error))errorBlock {
    __block NSError *error;
    NSArray<CoreArticle *> *array = [[self objContext] executeFetchRequest:[self fetchRequest] error:nil];
    if (array.count > 0) {
        [array enumerateObjectsUsingBlock:^(CoreArticle * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [[self objContext] deleteObject:obj];
            [[self objContext] save:&error];
        }];
    }
    if (errorBlock) {
        errorBlock(error);
    }
}

+ (NCArticle *)ncArticleWithCoreArticle:(CoreArticle *)cArticle {
    NCArticle *article = [[NCArticle alloc]init];
    article.article_addtime = cArticle.article_addtime;
    article.article_author = cArticle.article_author;
    article.article_content = cArticle.article_content;
    article.article_id = cArticle.article_id;
    article.article_title = cArticle.article_title;
    return article;
}

@end


